package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service(timeout = 100000)
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingDao shippingDao;
    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ExportDao exportDao;

    /**
     * 修改packing的状态
     * 修改packing下export(报运单)的状态
     * 修改shipping中的·各种状态(包括计算时效性 )
     * @param shipping
     */
    @Override
    public void save(Shipping shipping) {
        Packing packing = packingDao.selectByPrimaryKey(shipping.getShippingOrderId());
        //装箱时间
        Date packingTime = packing.getPackingTime();
        //计算效期
        if (packingTime != null) {
            // 使用默认时区和语言环境获得一个日历
            Calendar cal = Calendar.getInstance();
            cal.setTime(packingTime);
            //拿它的月份 然后加2 作为效性的日期
            cal.add(Calendar.MONTH,+2);
            shipping.setLimitDate(cal.getTime());
        }
        shipping.setCopyNum(String.valueOf(1));
        //设置状态 表示为草稿状态
        shipping.setState(0);
        //修改packing的状态 改成已上报
        packing.setState(1);
        //继续修改packing下的所有报运单的状态为4 已委托的状态
        String exportIdsString = packing.getExportIds();
        String[] exportIdsArray = exportIdsString.split(",");
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIdsArray));
        List<Export> exports = exportDao.selectByExample(exportExample);
        if (exports != null && exports.size() > 0) {
            exports.forEach(item -> {
                item.setState(4);
                exportDao.updateByPrimaryKeySelective(item);
            });
        }
        packingDao.updateByPrimaryKeySelective(packing);
        shippingDao.insert(shipping);
    }

    @Override
    public PageInfo<Shipping> findByPage(ShippingExample shippingExample, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippings = shippingDao.selectByExample(shippingExample);
        return new PageInfo<>(shippings);
    }

    @Override
    public List<Shipping> selectByExample(ShippingExample example) {
        return shippingDao.selectByExample(example);
    }

    @Override
    public Shipping selectByPrimaryKey(String shippingOrderId) {
        return shippingDao.selectByPrimaryKey(shippingOrderId);
    }

    @Override
    public Shipping toAddShipping(String packingListId) {
        Shipping shipping = new Shipping();
        Packing packing = packingDao.selectByPrimaryKey(packingListId);
        shipping.setShippingOrderId(packingListId);
        String exportIds = packing.getExportIds();
        String[] exportArray = exportIds.split(",");
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportArray));
        List<Export> exports = exportDao.selectByExample(exportExample);
        if (exports != null && exports.size() > 0) {
            //因为三个条件都是相同的才能装箱 所以这里只要获取一个数据的条件即可
            shipping.setPortOfLanding(exports.get(0).getShipmentPort());
            shipping.setPortOfDischarge(exports.get(0).getDestinationPort());
            //封装信用证号
            StringBuilder sb = new StringBuilder("");
            exports.forEach(item -> {
                sb.append(item.getLcno()).append(" ");
            });
            shipping.setLcNo(sb.toString());
        }
        //将packing中的marks 数据设置进去
        shipping.setMarks(packing.getMarks());
        return shipping;
    }

    /**
     * 改变装箱状态 packing
     * 改变packing下所有报运单的状态
     * 然后将自己 shipping删除掉
     * @param shippingOrderId
     */
    @Override
    public void delete(String shippingOrderId) {
        Packing packing = packingDao.selectByPrimaryKey(shippingOrderId);
        String exportIds = packing.getExportIds();
        if (!StringUtils.isEmpty(exportIds)) {
            String[] exportIdArray = exportIds.split(",");
            ExportExample exportExample = new ExportExample();
            exportExample.createCriteria().andIdIn(Arrays.asList(exportIdArray));
            List<Export> exports = exportDao.selectByExample(exportExample);
            exports.forEach(item -> {
                item.setState(3);
                exportDao.updateByPrimaryKeySelective(item);
            });
        }
        packing.setState(0);
        packingDao.updateByPrimaryKeySelective(packing);
        shippingDao.deleteByPrimaryKey(shippingOrderId);
    }

    @Override
    public List<Shipping> findAllShippingByReviewer(String reviewerId) {
        return shippingDao.findAllShippingByReviewer(reviewerId);
    }
}
